import json

import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib import pyplot as plt

with open('traffic_simulation.json','r+') as f:
    data_list = json.load(f)
    # # 提取所有不同的 time 值
    # unique_times = sorted(set(item["time"] for item in data_list))
    # print(unique_times)
    # all_to_all_data = [item for item in data_list if item['type'] == "all_to_all"]
    all_to_all_data = [item for item in data_list if item['type'] == "all_to_all" and item['timestamp'] == 0.0]
    # print(all_to_all_data[6])
    # 找到最大的 src 和 dst 值，用于确定数组的形状
    max_src = max(item["src"] for item in all_to_all_data)
    max_dst = max(item["dst"] for item in all_to_all_data)

    # 创建一个全零的数组，形状为 (max_src, max_dst)
    array = np.zeros((max_src, max_dst))

    # 将 size 的值填充到数组的对应位置
    for item in all_to_all_data:
            src = item["src"] - 1  # 因为数组索引从 0 开始
            dst = item["dst"] - 1
            # src = item["src"]  # 因为数组索引从 0 开始
            # dst = item["dst"]
            size = item["size"]
            array[src][dst] = size

    # print(array)
    # print(array[6][6])
    # print(array[6][0])
    # print(array[0][6])
    # print(len(array))
    # 绘制热力图
plt.figure(figsize=(8, 6))
sns.heatmap(array, annot=True, fmt=".2f", cmap="YlGnBu")
plt.title('Heatmap from JSON Data')
plt.xlabel('Destination')
plt.ylabel('Source')
plt.show()
